# encoding=utf8
'''
Created on Dec 28, 2015

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django.contrib import admin
from sdk_info.models import SDKInfo, SDKVersion
from sdk_info.forms import SDKInfoAdminForm, SDKVersionAdminFrom
from utils import field
from django.template.loader import get_template
from django.template.context import Context


class SDKVersionInline(admin.TabularInline):
    model = SDKVersion
    can_delete = False
    suit_classes = 'suit-tab suit-tab-version'
    verbose_name = ''
    verbose_name_plural = verbose_name
    form = SDKVersionAdminFrom

    class Meta:
        pass

    def get_formset(self, request, obj=None, **kwargs):
        if obj and obj.sdkversion_set.count() == 0:
            self.extra = 1
        else:
            self.extra = 0
        return admin.TabularInline.get_formset(self, request, obj=obj, **kwargs)


class SDKInfoAdmin(admin.ModelAdmin):
    list_per_page = 20
    form = SDKInfoAdminForm
    list_display = ('title', 'get_platform', 'get_versions', 'created_at',
                    'get_status')
    fieldsets = ((None, {'classes': ('suit-tab', 'suit-tab-info'),
                         'fields': ('title', 'platform', 'desc', 'status')}),
                 (None, {'classes': ('suit-tab', 'suit-tab-changelog'),
                         'fields': ('get_changelog',)}),)
    readonly_fields = ('get_changelog',)
    list_filter = ('status', 'platform')
    search_fields = ('title', 'desc')

    @field('Change Log', allow_tags=True)
    def get_changelog(self, obj):
        t = get_template('base/sdk_version_changelog.html')
        html = t.render(Context({'versions': obj.sdkversion_set.all()}))
        return html

    @field('Versions', allow_tags=True)
    def get_versions(self, obj):
        vs = obj.sdkversion_set.all()
        vs_list = ['{} - {}'.format(v.code, v.get_status_display()) for v in vs]
        return '<br />'.join(vs_list)

    @field('Platform', admin_order_field='platform')
    def get_platform(self, obj):
        return obj.get_platform_display()

    @field('Status', admin_order_field='status')
    def get_status(self, obj):
        return obj.get_status_display()

    def get_readonly_fields(self, request, obj=None):
        if not self.has_add_permission(request):
            return ('title', 'platform', 'desc', 'status', 'created_at',
                    'get_changelog')
        return admin.ModelAdmin.get_readonly_fields(self, request, obj=obj)

    def get_inline_instances(self, request, obj=None):
        if obj:
            if self.has_add_permission(request):
                self.inlines = [SDKVersionInline]
                self.suit_form_tabs = (('info', 'Info'), ('version', 'Version'),
                                       ('changelog', 'Change Log'))
            else:
                self.suit_form_tabs = (('info', 'Info'), ('changelog', 'Change Log'))
        else:
            self.inlines = []
            self.suit_form_tabs = (('info', 'Info'),)
        return admin.ModelAdmin.get_inline_instances(self, request, obj=obj)

    def get_actions(self, request):
        """
        remove delete action
        """
        actions = admin.ModelAdmin.get_actions(self, request)
        if actions.get('delete_selected'):
            del actions['delete_selected']
        return actions

    def save_model(self, request, obj, form, change):
        if 'status' in form.changed_data and obj.status == 'D':
            obj.sdkversion_set.update(status='D')
        obj.save()


admin.site.register(SDKInfo, SDKInfoAdmin)
